<?php
/**
 * 
 * 会员中心 
 * @author wj 
 * @date  2014-10-13
 *
 */
class TradeController extends HomeBaseController {
    public function _initialize() {
        parent::_initialize();
        $this->db = model('Order');
        $this->order_goods_db = model('OrderGoods');
    }
    //交易首页
    public function index1(){
        
    }

    public function test(){
        p(makeMinerOrderData(0));
    }


    // 交易中心
    public function index(){
        $_SESSION["send_code_time"]=random(10);
        // echo $_SESSION["send_code_time"]."<br>";
        // p($_SESSION);
        // p(strtotime(date("Y-m-d").C("site_timea")));
        // p(strtotime(date("Y-m-d").C("site_timeb")));
        $start_time = NOW_TIME - 86400 * 4;
        $end_time = NOW_TIME;
        $days = 5;
        $wallet = model('WalletPrice');
        for ($i=0; $i < $days; $i++) {
            $_time = $end_time - 86400 * $i;
            $_day = mdate($_time, 'Y-m-d');
            $sqlmap['_string'] = "DATE_FORMAT(FROM_UNIXTIME(time),'%Y-%m-%d') = '".$_day."'";
            $walletLists[mdate($_time, 'm-d')] = (float) $wallet->where($sqlmap)->getField('price');
        }
        $walletLists = array_reverse($walletLists, TRUE);
        $sell_days  = array_keys($walletLists);
        $series[]   = array('name' =>'DBT价格', 'data' => array_values($walletLists),'color'=>'#1b8ca5');
        $user = $this->userinfo;
        $user['cny'] = $user['user_money'] * C('site_cny_rate');
        $user['usd'] = $user['user_money'] * C('site_usd_rate');
        //当日价格
        $dayPrice = model('WalletPrice')->where(array('start'=>date('Y-m-d')))->getField('price');
        $today = model('WalletPrice')->where(array('start'=>date('Y-m-d')))->find();
        $yesterday = model('WalletPrice')->where(array('start'=>date('Y-m-d',strtotime(date('Y-m-d'))-86400)))->find();
        $range = bcdiv(($today['price']-$yesterday['price']),$yesterday['price'],4)*100;

        $buy_order_count = M("user_trade")->where("user_id>0")->count();

        $sell_order_count = M("user_trade")->where("sell_id>0")->count();
        // echo M()->getlastsql();
        $SEO = seo(0,"交易中心");
        include template('/user/trade/index');
    }
    // 交易中心交易列表
    public function tradelist(){
        if(IS_AJAX){
        $config = C('bla_config');
        $curpage = isset($_GET['curpage']) ? intval($_GET['curpage']) : 1;
        $page    = isset($_GET['page']) ? intval($_GET['page']) : 10;
        $sqlMap = array();
        // $sqlMap['user_id'] = $this->userid;
        $search_type = intval($_GET['type']);
        if ($search_type > 0 ) $sqlMap['type'] = $search_type;
        $sqlMap['ut.status'] = 1;

        if($sqlMap['type']==1) $sqlMap['type']=666;
        if($_GET['phone']!="") $sqlMap['mobile_phone']=$_GET['phone'];

        $rowsResult = (array) M("user_trade ut")
        ->field("u.username,u.mobile_phone,u.ico,ut.*")
        ->join("__USER__ u on u.id=ut.user_id")
        ->where($sqlMap)->limit($page)->page($curpage)->order('id asc')->select();
        // echo M()->getlastsql();
        if (!empty($rowsResult)) {
            foreach ($rowsResult as $key => $value) {
                $rowsResult[$key]['ico'] = get_avatar($value['ico']);
               // $rowsResult[$key]['logtime'] = date('Y-m-d H:i:s',$value['dateline']);
               // $rowsResult[$key]['typetxt'] = $config[$value['msg_type']];
            }
        }
        $rowsCount  = (int) M("user_trade ut")->where($sqlMap)->count();
        // echo M()->getlastsql();

        $output = array();
        $output['datas']['list'] = $rowsResult;
        // var_dump($rowsCount);
        // var_dump($page);
        // var_dump($curpage);
        // var_dump(ceil($rowsCount / $page ) > $curpage);
        $output['hasmore'] = (ceil($rowsCount / $page ) > $curpage);
        echo json_encode($output);exit;
        }
    }


    
}